<?php 
/** 
 * 题目：反转链表
 * 
 * 输入一个链表，反转链表后，输出新链表的表头。
 */

class ListNode{
    var $val;
    var $next = NULL;
    function __construct($x){
        $this->val = $x;
    }
}

function ReverseList($pHead)
{
    $pre = null;
    $curNode = $pHead;
    while ($curNode!=null) {
    	$next = $curNode->next;
    	$curNode->next = $pre;
    	$pre = $curNode;
        $curNode = $next;
    }
    return $pre;
}

$head = new ListNode(1);
$head->next = new ListNode(2);
$head->next->next = new ListNode(3);
$head->next->next->next = new ListNode(4);
$head->next->next->next->next = new ListNode(5);

$node = ReverseList($head);
while ( !is_null($node) ) {
	echo $node->val.PHP_EOL;
	$node = $node->next;
}